<template>
  <q-header elevated class="header-wrapper flex justify-between">
    <div v-if="isHuaTian">
      <img style="height: 36px" src="../../statics/img/haosen.png" alt="">
    </div>
    <div v-else-if="isZhongji">
      <div class="name cursor-pointer" @click="goHome">南方中集</div>
    </div>
    <div v-else class="flex">
      <div @click="goHome" class="system-logo" v-if="systemLogo">
        <q-img
          class="logo"
          fit="contain"
          :src="systemLogo"
          spinner-color="white"
        />
      </div>
      <template v-else>
        <i class="iconfont icontubiao-76 logo cursor-pointer" @click="goHome"></i>
        <div class="name cursor-pointer" @click="goHome">语祯云工</div>
      </template>
    </div>
    <div class="flex items-center">
      <div class="cursor-pointer margin-r-30" @click="$router.push({path: '/main/home/news/app-manage'})">
        应用管理
      </div>
      <a-dropdown class="margin-r-30" v-if="basicDataMenu.length > 0">
        <div class="cursor-pointer" @click="e => e.preventDefault()">
          基础数据<a-icon class="margin-l-5" type="caret-down" />
        </div>
        <div slot="overlay">
          <a-menu v-model="basicData" @click="handleMenuClick">
            <a-menu-item v-for="item in basicDataMenu" :key="item.link">{{ item.name }}</a-menu-item>
          </a-menu>
        </div>
      </a-dropdown>
      <a-dropdown v-if="configDataMenu.length > 0">
        <div class="cursor-pointer" @click="e => e.preventDefault()">
          设置管理<a-icon class="margin-l-5" type="caret-down" />
        </div>
        <div slot="overlay">
            <a-menu v-model="configData" @click="handleConfigMenuClick">
              <a-menu-item v-for="item in configDataMenu" :key="item.link">{{ item.name }}</a-menu-item>
            </a-menu>
        </div>
      </a-dropdown>
      <div class="separate-wrapper">
        <div class="separate"></div>
      </div>
      <a-badge class="cursor-pointer" v-if="permissions.includes(115)" :dot="msgCount > 0" @click="$emit('messageClick', '/main/factory/mine-message')"><a-icon type="mail" style="font-size: 16px" /></a-badge>

      <a-dropdown>
        <div class="cursor-pointer">
          <a-avatar style="margin-left: 20px;margin-top: -5px" :size="24" :src="userPhoto ? userPhoto : `../../../../statics/img/default.png`" />
          <a-icon class="margin-l-5" type="caret-down" />
        </div>
        <div slot="overlay">
          <div class="user-info">
            <div class="name">{{ userDetail.nickName }}</div>
            <div class="phone">手机号:{{ userDetail.phone }}</div>
          </div>
          <a-menu class="user" mode="inline" @click="handleAccountMenuClick">
            <a-menu-item key="user" v-if="userInfo.tenant_id !== 0">
              账户信息
            </a-menu-item>
            <a-menu-item key="company" v-if="isAdministrator">
              <div class="flex">
                <div>企业认证</div>
                <div :style="getStyle(userDetail.status)">【{{userDetail.status}}】</div>
              </div>
            </a-menu-item>
            <a-menu-item v-if="!isIMC" key="logout">注销</a-menu-item>
          </a-menu>
        </div>
      </a-dropdown>
    </div>
  </q-header>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import _ from 'lodash'
import { menuListData } from '../../assets/js/menu-list'
import { logout } from '../../boot/login/login'
import { removeRefreshToken, removeToken } from 'yz-help'
import { goLoginPage } from '../../permission'
import { getUserDetail } from '../../boot/home/home-new'

export default {
  name: 'layout-header',
  data () {
    return {
      basicData: [],
      configData: [],
      basicDataMenu: [],
      configDataMenu: []
    }
  },
  computed: {
    ...mapGetters({
      'userInfo': 'userInfo',
      'userDetail': 'userDetail',
      'permissions': 'permissions',
      'userPhoto': 'userPhoto',
      'msgCount': 'mes/msgCount',
      'systemLogo': 'systemLogo'
    }),
    isHuawei () {
      return this.userInfo.channel === 'hw' && this.userInfo.client_id === 'huawei_iot_stage_app'
    },
    isHuaTian () {
      if (this.userInfo.tenant_id === '48207560919760896') {
        document.title = '豪森智源'
        document.querySelector('link[rel="icon"]').href = 'favicon.ico'
      }
      return this.userInfo.tenant_id === '48207560919760896'
    },
    isZhongji () {
      if (this.userInfo.tenant_id === '49762054270242817') {
        document.title = '南方中集'
        document.querySelector('link[rel="icon"]').href = 'favicon.ico'
      }
      return this.userInfo.tenant_id === '49762054270242817'
    },
    isYQZZ () {
      return this.userInfo.channel === 'hw' && this.userInfo.client_id === process.env.YQZZ_CLIENT_ID
    },
    isIMC () {
      return this.userInfo.client_id === process.env.IMC_CLIENT_ID
    },
    isAdministrator () {
      return this.userDetail.isAdministrator || this.userInfo.tenant_id === 0
    }
  },
  created () {
    this.changeMenu()
    this.getUserDetailInfo()
  },
  methods: {
    getUserDetailInfo () {
      getUserDetail().then(res => {
        const type = {
          '0': '审核中',
          '1': '已认证',
          '2': '认证失败'
        }
        const data = { ...res.data, status: this.userInfo.tenant_id === 0 ? '未认证' : type[res.data.qualificationAuditStatus] }
        this.set_userDetail(data)
      })
    },
    // 基础数据菜单点击
    handleMenuClick (e) {
      const link = e.key
      this.$router.push({ path: link })
      this.$emit('basicClick', link)
      // 清除active
      this.$nextTick(() => {
        this.basicData = []
      })
    },
    // 设置管理菜单点击
    handleConfigMenuClick (e) {
      const link = e.key
      this.$router.push({ path: link })
      this.$emit('configClick', link)
      // 清除active
      this.$nextTick(() => {
        this.configData = []
      })
    },
    // 账户菜单点击
    handleAccountMenuClick (e) {
      const key = e.key
      if (key === 'user') {
        this.$router.push({ path: '/main/user' })
      } else if (key === 'company') {
        this.$router.push({ path: '/main/home/news/company-indent' })
      } else if (key === 'logout') {
        this.logout()
      }
    },
    // 菜单栏
    changeMenu () {
      // 获取一级菜单
      const menuPage = Object.entries(this.Enums.SYS_PERMISSION)
        .filter(entry => entry[1].type === 'PAGE' && this.permissions.indexOf(entry[1].value) > -1)
      const basicData = []
      const configData = []
      const menuData = _.cloneDeep(menuListData)
      const basicMenuLink = menuData.find(item => item.desc === this.Enums.SYS_PERMISSION.SYS_MENU.name)
      const configMenuLink = menuData.find(item => item.desc === this.Enums.SYS_PERMISSION.FACTORY_MENU.name)
      menuPage.forEach(item => {
        const info = item[1]
        // 如果parentId为基础数据的id
        if (info.parentId === this.Enums.SYS_PERMISSION.SYS_MENU.value) {
          // 通过desc匹配link
          const linkList = basicMenuLink.children.find(item => item.desc === info.name)
          basicData.push(linkList)
        }
        if (info.parentId === this.Enums.SYS_PERMISSION.FACTORY_MENU.value) {
          // 通过desc匹配link
          const linkList = configMenuLink.children.find(item => item.desc === info.name)
          configData.push(linkList)
        }
      })
      this.basicDataMenu = basicData
      this.configDataMenu = configData.filter(item => {
        return item.desc !== this.Enums.SYS_PERMISSION.MINE_MESSAGE_PAGE.name && item.desc !== this.Enums.SYS_PERMISSION.MESSAGE_SET_PAGE.name
      })
    },
    // 断开连接
    disconnect () {
      if (this.client) {
        this.client.disconnect(() => {
          console.log('断开连接')
        })
      }
    },
    // 退出登录
    logout () {
      logout().finally(() => {
        removeToken()
        removeRefreshToken()
        this.disconnect()
        if (this.isHuawei) {
          window.location.href = `https://auth.iotstage.huaweicloud.com/logout?service=${process.env.DLP_PATH}?utm_source=huawei`
        } else if (this.isYQZZ) {
          window.location.href = `${process.env.YQZZ_AUTH_PAGE}/cas/logout?loginType=2&service=${process.env.DLP_PATH}?utm_source=yqzz`
        } else {
          goLoginPage()
        }
      })
    },
    // 去首页
    goHome () {
      if (this.isHuawei) {
        window.open(process.env.PLATFORM_PATH + '?utm_source=huawei', '_blank')
      } else if (this.isYQZZ) {
        window.open(process.env.PLATFORM_PATH + '?utm_source=yqzz', '_blank')
      } else {
        window.open(process.env.PLATFORM_PATH, '_blank')
      }
    },
    goConsole () {
      if (this.isHuawei) {
        window.open(process.env.PLATFORM_PATH + 'console/overview' + '?utm_source=huawei', '_blank')
      } else if (this.isYQZZ) {
        window.open(process.env.PLATFORM_PATH + 'console/overview' + '?utm_source=yqzz', '_blank')
      } else {
        window.open(process.env.PLATFORM_PATH + 'console/overview', '_blank')
      }
    },
    // 获取颜色
    getStyle (status) {
      let style = 'color: blue'
      switch (status) {
        case '已认证':
          style = 'color: #18D77F'
          break
        case '审核中':
          style = 'color: #5F77FD'
          break
        case '认证失败':
          style = 'color: #666666'
          break
        case '未认证':
          style = 'color: #E43434'
          break
      }
      return style
    },
    ...mapMutations({
      'set_userDetail': 'SET_USERDETAIL' // 更改权限数组
    })
  }
}
</script>

<style scoped lang="scss">
/deep/ .ant-menu,
/deep/ .ant-menu-item:active, .ant-menu-submenu-title:active, {
  background: #151822;
  color: #ADB0B8;
}
.user {
  border-top: 1px solid #40434E;
}
/deep/ .ant-menu:not(.ant-menu-horizontal) .ant-menu-item-selected{
  background: #2D313C;
  color: #ADB0B8
}
/deep/.ant-menu-vertical .ant-menu-item::after, .ant-menu-vertical-left .ant-menu-item::after, .ant-menu-vertical-right .ant-menu-item::after, .ant-menu-inline .ant-menu-item::after{
  border-right: none;
}
/deep/.ant-menu-vertical .ant-menu-item:not(:last-child) {
  margin-top: 0;
  margin-bottom: 0;
}

.user-info{
  background: #151822;
  color: #ADB0B8;
  padding-left: 24px;
  height: 65px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  .name {
    font-size: 14px;
    font-weight: 400;
    line-height: 2;
  }
  .phone {
    font-size: 12px;
    padding-right: 20px;
  }
}
.header-wrapper {
  padding: 0 20px;
  width: 100%;
  height: 50px;
  line-height: 50px;
  background: #1E222D;
  box-shadow: 0px 5px 5px 0px rgba(6, 6, 6, 0.15);
  color: #ADB0B8;
  min-width: 700px;

  .logo{
    font-size: 36px;
    color: #FFFFFF;
  }

  .name{
    font-size: 16px;
    font-weight: bold;
    color: #FFFFFF;
    margin-left: 10px;
  }

  .separate-wrapper{
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 16px;

    .separate {
      width: 1px;
      height: 16px;
      background: #53555C;
    }
  }

  .margin-r-30{
    margin-right: 30px;
  }

  .margin-l-5{
    margin-left: 5px;
  }

  .msg-circle{
    height: 8px;
    width: 8px;
    background: red;
    border-radius: 50%;
    position: absolute;
    right: 5px;
    top: 5px;
  }
}
</style>
<style scoped lang="stylus">
.system-logo{
  width: 150px;
  height: 40px;
  cursor pointer
  .logo{
    width: 150px;
    height: 40px;
    >>>.q-img__image{
      background-size: contain !important;
    }
  }
}
</style>
